package com.ruoyi.common.oneclicklogin;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;
import org.apache.commons.codec.digest.DigestUtils;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.ProjectConstant;

/**
 * 	一键登录demo
 * @author DEREK
 *
 */
public class OneClickLoginDemo {

	/**
	 * com.netease.yidun.sdk.core.exception.YidunSdkException: No available domain. productCode=mobileverify, regionCode=cn-hangzhou
	at com.netease.yidun.sdk.core.endpoint.AbstractEndpointResolver.resolve(AbstractEndpointResolver.java:27)
	at com.netease.yidun.sdk.core.client.DefaultClient$Context.resolveDomainForFirstAttempt(DefaultClient.java:349)
	at com.netease.yidun.sdk.core.client.DefaultClient$Context.resolveDomain(DefaultClient.java:340)
	at com.netease.yidun.sdk.core.client.DefaultClient$Context.createRequest(DefaultClient.java:308)
	at com.netease.yidun.sdk.core.client.DefaultClient.execute(DefaultClient.java:212)
	at com.ruoyi.common.oneclicklogin.OneClickLoginClient.getMobileNumber(OneClickLoginClient.java:42)
	at com.ruoyi.common.oneclicklogin.OneClickLoginDemo.main(OneClickLoginDemo.java:28)

	 * @param args
	 */
	public static void main(String[] args) {
		// 实例化发起请求的client对象
		OneClickLoginClient oneClickLoginClient = new OneClickLoginClient(ProjectConstant.NETEASE_SECRET_ID, ProjectConstant.NETEASE_SECRET_KEY);
		// 如需自定义请求器参数，可使用参考如下方式创建ClientProfile，然后创建client
		//	        ClientProfile profile = createProfile("secretId", "secretKey");
		//	        OneClickLoginClient client = new OneClickLoginClient(profile);
		// 填充参数
		MobileNumberGetRequest request = new MobileNumberGetRequest(ProjectConstant.NETEASE_BUSINESS_ID)
				// 【不可重复使用】易盾token(有效期2分钟)
				.token("87159e0c49ce4f3ba2b28face48cdbf01")
				// 【不可重复使用】运营商授权码(有效期2分钟)
				.accessToken("7b22616363657373546f6b656e223a226e6d6366326437376662616138653438393261393765303766636338626336643664222c22677741757468223a2234373238227d");

		MobileNumberGetResponse response = null;
		try {
			System.out.println(request.toString());
			response = oneClickLoginClient.getMobileNumber(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("结果："+response.toString());
		if (response != null && response.getCode() == 200) {
			MobileNumberResult data = response.getData();
			// data 为手机号取号结果，具体字段详细信息可参考官网文档
			System.out.println("返回数据：");
			System.out.println(JSONObject.toJSONString(data));
		}
	}

	/**
	 * 生成签名信息
	 * @param secretKey 产品私钥
	 * @param params 接口请求参数名和参数值map，不包括signature参数名
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	public static String genSignature(String secretKey, Map<String, String> params) throws UnsupportedEncodingException{
		// 1. 参数名按照ASCII码表升序排序
		String[] keys = params.keySet().toArray(new String[0]);
		Arrays.sort(keys);

		// 2. 按照排序拼接参数名与参数值
		StringBuilder sb = new StringBuilder();
		for (String key : keys) {
			sb.append(key).append(params.get(key));
		}
		// 3. 将secretKey拼接到最后
		sb.append(secretKey);

		// 4. MD5是128位长度的摘要算法，转换为十六进制之后长度为32字符
		return DigestUtils.md5Hex(sb.toString().getBytes("UTF-8"));
	}
}
